// components/select-tree/index.js

import { Target, HttpUtil, Toast } from '../../utils/http_util.js'
import { camelCase, objectKey2CamelCase } from '../../utils/util.js'
import { ServiceName, callService } from '../../utils/store.js'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    requireLeaf: false,
    url: '',
    idKey: '',
    valueKey: '',
    pidKey: '',
    rootPid: '',
    currentPid: '',
    rootTagCode: '',
    rootItemTagCode: '',
    param: [],
    dataList: [],
    selectedList: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let param = Object.assign({}, options)
    delete param.requireLeaf
    delete param.url
    delete param.idKey
    delete param.valueKey
    delete param.pidKey
    delete param.rootPid
    delete param.currentPid
    delete param.rootTagCode
    delete param.rootItemTagCode
    delete param.title
    
    this.setData({
      requireLeaf: options.requireLeaf || false,
      url: options.url || '',
      idKey: options.idKey,
      valueKey: options.valueKey,
      pidKey: options.pidKey,
      rootPid: options.rootPid,
      currentPid: options.rootPid,
      rootTagCode: options.rootTagCode || '',
      rootItemTagCode: options.rootItemTagCode || '',
      param: param
    })

    wx.setNavigationBarTitle({
      title: options.title? decodeURIComponent(options.title) : '请选择...',
    })

    if (this.data.rootItemTagCode && this.data.rootItemTagCode.length) {
      this.fetchRootItem()
      this.fetchData()
    } else {
      const def = {}
      def[this.data.idKey] = ''
      def[this.data.valueKey] = '全部'
      this.data.selectedList.push(def)
      this.setData({
        selectedList: this.data.selectedList
      })
      this.fetchData(this.data.rootTagCode)
    }
    
  },

  fetchRootItem() {
    let para = Object.assign({}, this.data.param)
    para.tag_code = this.data.rootItemTagCode
    para.ticket = HttpUtil.ticket
    para[this.data.idKey] = this.data.rootPid
    Toast.showLoading()
    HttpUtil.get(Target.self, '/comtag/selectComTagDataList.action', para).then(res => {
      res.data.data_list.map(item => {
        objectKey2CamelCase(item)
      })
      if (!res.data.data_list.length) {
        Toast.message('暂无当前根节点')
        return
      }
      this.data.selectedList.push(res.data.data_list[0])
      this.setData({
        selectedList: this.data.selectedList
      })
    })
  },

  fetchData(tagCode) {
    let para = Object.assign({}, this.data.param)
    para.ticket = HttpUtil.ticket
    para[this.data.pidKey] = this.data.currentPid
    if(tagCode) {
      para.tag_code = tagCode
    }
    Toast.showLoading()
    HttpUtil.get(Target.self, '/comtag/selectComTagDataList.action', para).then(res => {
      res.data.data_list.map(item => {
        objectKey2CamelCase(item)
      })
      this.setData({
        dataList: res.data.data_list
      })
    })
  },

  itemClicked(e) {
    let item = e.currentTarget.dataset.item

    let selectedList = this.data.selectedList
    if (selectedList.length > 0 && 
        selectedList[selectedList.length-1].isLeaf > 0) {
      selectedList.splice(selectedList.length-1, 1, item)
    } else {
      this.data.selectedList.push(item)
    }

    this.setData({
      currentPid: item[this.data.idKey],
      selectedList: this.data.selectedList
    })
    if(!item.isLeaf) {
      this.fetchData()
    }
  },

  selectedBtnClicked(e) {
    let item = e.currentTarget.dataset.item
    if (!item.isLeaf) {
      let index = this.data.selectedList.indexOf(item)
      let valueKey = this.data.valueKey

      this.data.selectedList.map((d, i) => {
        if (d[valueKey] == item[valueKey]) {
          index = i
        }
      })
      this.data.selectedList = this.data.selectedList.slice(0, index + 1)
      this.setData({
        currentPid: item[this.data.idKey],
        selectedList: this.data.selectedList
      })
      if(this.data.currentPid && this.data.currentPid.length) {
        this.fetchData()
      } else {
        this.fetchData(this.data.rootTagCode)
      }
    }
    console.log(item)
  },

  submit() {
    let item = this.data.selectedList[this.data.selectedList.length - 1]
    if (this.data.requireLeaf && !item.isLeaf) {
      Toast.message('请选择子节点')
      return
    }
    if(!item[this.data.idKey] || item[this.data.idKey] == '') {
      wx.navigateBack()
      return
    }
    callService(ServiceName.selectTreeCallback, item)
    wx.navigateBack()
  }
})